/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tablero.java
 *
 * Created on Jul 7, 2009, 8:46:34 PM
 */
package cuatroenraya.gui;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author mateo
 */
public class Tablero extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;
	private EstadoTablero estado = EstadoTablero.INICIO;
	private JButton botonElegido = null;
    private int jugadorHumano = 1;
    private int casillerosLibres = 5;
    private int jugadasHechas = 0;
    private Thread mainThread;
    int[][] tablero;

    /** Creates new form Tablero */
    public Tablero() {
        initComponents();
        try {
    	UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
    	SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
        	
        }
    }

    public void llenarCasilleros(int[][] tablero) {
        // Obtener el panel donde se encuentran los botones.
        this.tablero = tablero;
        JPanel panel = (JPanel) ((javax.swing.JLayeredPane) this.getRootPane().getComponents()[1]).getComponent(0);

        jugadasHechas = 0;
        for(int i = 0; i < 8; i++) {
        	for(int j = 0; j < 8; j++) {
        		if(tablero[i][j] == jugadorHumano) {
                    jugadasHechas++;
                }
        	}
        }
        
        if(estado == EstadoTablero.INICIO) {
        	estado = jugadasHechas < casillerosLibres ? EstadoTablero.ESPERANDO_ELECCION_BLANCO : EstadoTablero.ESPERANDO_ELECCION_FICHA;
        }

        switch (estado) {
		case ESPERANDO_ELECCION_BLANCO:
			setEnabledBlanks(true);
			break;
		case ESPERANDO_ELECCION_FICHA:
		case ESPERANDO_ELECCION_NUEVA_POSICION:
			setEnabledBlanks(false);
			break;
		}
        
        // Llenar el tablero con las imagenes que corresponden.        
        for (int i = 0; i < 64; i++) {
            Component button = null;

            while (button == null) {
                for (Component component : panel.getComponents()) {
                    if ((component instanceof javax.swing.JButton) && (component.getName().equals("" + i))) {
                        button = component;
                        break;
                    }
                }
            }
            int jugadorEnCasilla = tablero[i / 8][i % 8];
            colocarImagen((javax.swing.JButton) button, jugadorEnCasilla);
        }
    }

    private void colocarImagen(javax.swing.JButton boton, int valor) {
        switch (valor) {
        case (0):
            boton.setIcon(null); // NOI18N
            boton.setDisabledIcon(null); // NOI18N        
            if(estado == EstadoTablero.ESPERANDO_ELECCION_NUEVA_POSICION) {
        		boton.setEnabled(esVecino(botonElegido, boton));
        	}
        break;
        case (1):
        	boton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/circle.png"))); // NOI18N
    		boton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/circle.png"))); // NOI18N
        	switch (estado) {
    		case ESPERANDO_ELECCION_BLANCO:
    			boton.setEnabled(false);
    			break;
    		case ESPERANDO_ELECCION_FICHA:
    			boton.setEnabled(jugadorHumano == valor);
    			break;
    		case ESPERANDO_ELECCION_NUEVA_POSICION:        			
    			if(boton.equals(botonElegido)) {
        			boton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/spiral.png"))); // NOI18N
                	boton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/spiral.png"))); // NOI18N
                	boton.setEnabled(true);
    			} else {
    				boton.setEnabled(false);
    			}
    		default:
    			break;
    		}
            break;
        case (2):
            boton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/cross.png"))); // NOI18N                
            boton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/cross.png"))); // NOI18N                
            switch (estado) {
    		case ESPERANDO_ELECCION_BLANCO:
    			boton.setEnabled(false);
    			break;
    		case ESPERANDO_ELECCION_FICHA:
    			boton.setEnabled(jugadorHumano == valor);
    			break;
    		case ESPERANDO_ELECCION_NUEVA_POSICION:
    			if(boton.equals(botonElegido)) {
        			boton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/spiral.png"))); // NOI18N
                	boton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/cuatroenraya/gui/spiral.png"))); // NOI18N
                	boton.setEnabled(true);
    			} else {
    				boton.setEnabled(false);
    			}
    		default:
    			break;
    		}
            break;
        }

        boton.setMaximumSize(new java.awt.Dimension(27, 27));
        boton.setPreferredSize(new java.awt.Dimension(33, 33));
        boton.setMinimumSize(new java.awt.Dimension(27, 27));
    }
    
    /**
     * Retorna true si está en blanco, y está próximo.
     * @return
     */
    private boolean esVecino(JButton boton, JButton posibleVecino) {
    	final int v = Integer.parseInt(posibleVecino.getName());
    	final int vX = v / 8;
    	final int vY = v % 8;
    	
    	if(tablero[vX][vY] == 0) {	    	
	    	final int o = Integer.parseInt(boton.getName());
	    	final int oX = o / 8;
	    	final int oY = o % 8;
	    	return Math.abs(vX - oX) <= 1 && Math.abs(vY - oY) <= 1;
    	} else {
    		return false;
    	}
    }
    
    private void setEnabledBlanks(boolean enable) {
    	JPanel panel = (JPanel) ((javax.swing.JLayeredPane) this.getRootPane().getComponents()[1]).getComponent(0);
        for (int i = 0; i < 64; i++) {
            Component button = null;

            while (button == null) {
                for (Component component : panel.getComponents()) {
                    if ((component instanceof javax.swing.JButton) && (component.getName().equals("" + i))) {
                        button = component;
                        break;
                    }
                }
            }
            
            if(tablero[i / 8][i % 8] == 0) {
                button.setEnabled(enable);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();
        jButton17 = new javax.swing.JButton();
        jButton18 = new javax.swing.JButton();
        jButton19 = new javax.swing.JButton();
        jButton20 = new javax.swing.JButton();
        jButton21 = new javax.swing.JButton();
        jButton22 = new javax.swing.JButton();
        jButton23 = new javax.swing.JButton();
        jButton24 = new javax.swing.JButton();
        jButton25 = new javax.swing.JButton();
        jButton26 = new javax.swing.JButton();
        jButton27 = new javax.swing.JButton();
        jButton28 = new javax.swing.JButton();
        jButton29 = new javax.swing.JButton();
        jButton30 = new javax.swing.JButton();
        jButton31 = new javax.swing.JButton();
        jButton32 = new javax.swing.JButton();
        jButton33 = new javax.swing.JButton();
        jButton34 = new javax.swing.JButton();
        jButton35 = new javax.swing.JButton();
        jButton36 = new javax.swing.JButton();
        jButton37 = new javax.swing.JButton();
        jButton38 = new javax.swing.JButton();
        jButton39 = new javax.swing.JButton();
        jButton40 = new javax.swing.JButton();
        jButton41 = new javax.swing.JButton();
        jButton42 = new javax.swing.JButton();
        jButton43 = new javax.swing.JButton();
        jButton44 = new javax.swing.JButton();
        jButton45 = new javax.swing.JButton();
        jButton46 = new javax.swing.JButton();
        jButton47 = new javax.swing.JButton();
        jButton48 = new javax.swing.JButton();
        jButton49 = new javax.swing.JButton();
        jButton50 = new javax.swing.JButton();
        jButton51 = new javax.swing.JButton();
        jButton52 = new javax.swing.JButton();
        jButton53 = new javax.swing.JButton();
        jButton54 = new javax.swing.JButton();
        jButton55 = new javax.swing.JButton();
        jButton56 = new javax.swing.JButton();
        jButton57 = new javax.swing.JButton();
        jButton58 = new javax.swing.JButton();
        jButton59 = new javax.swing.JButton();
        jButton60 = new javax.swing.JButton();
        jButton61 = new javax.swing.JButton();
        jButton62 = new javax.swing.JButton();
        jButton63 = new javax.swing.JButton();
        jButton64 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Un buen Cuatro en Raya");
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridLayout(8, 8, 1, 1));

        jButton1.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton1.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton1.setName("0"); // NOI18N
        jButton1.setPreferredSize(new java.awt.Dimension(49, 49));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);

        jButton2.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton2.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton2.setName("1"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);

        jButton3.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton3.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton3.setName("2"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);

        jButton4.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton4.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton4.setName("3"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton4);

        jButton5.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton5.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton5.setName("4"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton5);

        jButton6.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton6.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton6.setName("5"); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton6);

        jButton7.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton7.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton7.setName("6"); // NOI18N
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton7);

        jButton8.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton8.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton8.setName("7"); // NOI18N
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton8);

        jButton9.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton9.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton9.setName("8"); // NOI18N
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton9);

        jButton10.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton10.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton10.setName("9"); // NOI18N
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton10);

        jButton11.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton11.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton11.setName("10"); // NOI18N
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton11);

        jButton12.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton12.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton12.setName("11"); // NOI18N
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton12);

        jButton13.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton13.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton13.setName("12"); // NOI18N
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton13);

        jButton14.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton14.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton14.setName("13"); // NOI18N
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton14);

        jButton15.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton15.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton15.setName("14"); // NOI18N
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton15);

        jButton16.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton16.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton16.setName("15"); // NOI18N
        jButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton16);

        jButton17.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton17.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton17.setName("16"); // NOI18N
        jButton17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton17);

        jButton18.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton18.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton18.setName("17"); // NOI18N
        jButton18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton18);

        jButton19.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton19.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton19.setName("18"); // NOI18N
        jButton19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton19);

        jButton20.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton20.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton20.setName("19"); // NOI18N
        jButton20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton20);

        jButton21.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton21.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton21.setName("20"); // NOI18N
        jButton21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton21);

        jButton22.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton22.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton22.setName("21"); // NOI18N
        jButton22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton22);

        jButton23.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton23.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton23.setName("22"); // NOI18N
        jButton23.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton23);

        jButton24.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton24.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton24.setName("23"); // NOI18N
        jButton24.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton24);

        jButton25.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton25.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton25.setName("24"); // NOI18N
        jButton25.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton25);

        jButton26.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton26.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton26.setName("25"); // NOI18N
        jButton26.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton26);

        jButton27.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton27.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton27.setName("26"); // NOI18N
        jButton27.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton27);

        jButton28.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton28.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton28.setName("27"); // NOI18N
        jButton28.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton28);

        jButton29.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton29.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton29.setName("28"); // NOI18N
        jButton29.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton29);

        jButton30.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton30.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton30.setName("29"); // NOI18N
        jButton30.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton30);

        jButton31.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton31.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton31.setName("30"); // NOI18N
        jButton31.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton31);

        jButton32.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton32.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton32.setName("31"); // NOI18N
        jButton32.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton32);

        jButton33.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton33.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton33.setName("32"); // NOI18N
        jButton33.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton33);

        jButton34.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton34.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton34.setName("33"); // NOI18N
        jButton34.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton34);

        jButton35.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton35.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton35.setName("34"); // NOI18N
        jButton35.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton35);

        jButton36.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton36.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton36.setName("35"); // NOI18N
        jButton36.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton36);

        jButton37.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton37.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton37.setName("36"); // NOI18N
        jButton37.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton37);

        jButton38.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton38.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton38.setName("37"); // NOI18N
        jButton38.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton38);

        jButton39.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton39.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton39.setName("38"); // NOI18N
        jButton39.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton39);

        jButton40.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton40.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton40.setName("39"); // NOI18N
        jButton40.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton40);

        jButton41.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton41.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton41.setName("40"); // NOI18N
        jButton41.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton41);

        jButton42.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton42.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton42.setName("41"); // NOI18N
        jButton42.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton42);

        jButton43.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton43.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton43.setName("42"); // NOI18N
        jButton43.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton43);

        jButton44.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton44.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton44.setName("43"); // NOI18N
        jButton44.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton44);

        jButton45.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton45.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton45.setName("44"); // NOI18N
        jButton45.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton45);

        jButton46.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton46.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton46.setName("45"); // NOI18N
        jButton46.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton46);

        jButton47.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton47.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton47.setName("46"); // NOI18N
        jButton47.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton47);

        jButton48.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton48.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton48.setName("47"); // NOI18N
        jButton48.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton48);

        jButton49.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton49.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton49.setName("48"); // NOI18N
        jButton49.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton49);

        jButton50.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton50.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton50.setName("49"); // NOI18N
        jButton50.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton50);

        jButton51.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton51.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton51.setName("50"); // NOI18N
        jButton51.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton51);

        jButton52.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton52.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton52.setName("51"); // NOI18N
        jButton52.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton52);

        jButton53.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton53.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton53.setName("52"); // NOI18N
        jButton53.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton53);

        jButton54.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton54.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton54.setName("53"); // NOI18N
        jButton54.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton54);

        jButton55.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton55.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton55.setName("54"); // NOI18N
        jButton55.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton55);

        jButton56.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton56.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton56.setName("55"); // NOI18N
        jButton56.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton56);

        jButton57.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton57.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton57.setName("56"); // NOI18N
        jButton57.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton57);

        jButton58.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton58.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton58.setName("57"); // NOI18N
        jButton58.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton58);

        jButton59.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton59.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton59.setName("58"); // NOI18N
        jButton59.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton59);

        jButton60.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton60.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton60.setName("59"); // NOI18N
        jButton60.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton60);

        jButton61.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton61.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton61.setName("60"); // NOI18N
        jButton61.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton61);

        jButton62.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton62.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton62.setName("61"); // NOI18N
        jButton62.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton62);

        jButton63.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton63.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton63.setName("62"); // NOI18N
        jButton63.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton63);

        jButton64.setMaximumSize(new java.awt.Dimension(27, 27));
        jButton64.setMinimumSize(new java.awt.Dimension(27, 27));
        jButton64.setName("63"); // NOI18N
        jButton64.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Tablero.this.actionPerformed(evt);
            }
        });
        getContentPane().add(jButton64);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void actionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_actionPerformed
    {//GEN-HEADEREND:event_actionPerformed
    	int posicionReciente = Integer.parseInt(((JButton) evt.getSource()).getName());
    	switch (estado) {
		case ESPERANDO_ELECCION_BLANCO:
			estado = EstadoTablero.INICIO;
			this.elegirPosicion(-1, posicionReciente);
			break;
		case ESPERANDO_ELECCION_FICHA:
			setEnabledBlanks(false);
            botonElegido = (JButton) evt.getSource();
            estado = EstadoTablero.ESPERANDO_ELECCION_NUEVA_POSICION;
            llenarCasilleros(tablero);
			break;
		case ESPERANDO_ELECCION_NUEVA_POSICION:
			if(evt.getSource().equals(botonElegido)) {
				botonElegido = null;
				estado = EstadoTablero.ESPERANDO_ELECCION_FICHA;
				llenarCasilleros(tablero);
			} else {
				int posicionPrevia = Integer.parseInt(((JButton) botonElegido).getName());
				estado = EstadoTablero.INICIO;
	            this.elegirPosicion(posicionPrevia, posicionReciente);
			}
			break;
		}
    }//GEN-LAST:event_actionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Tablero().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton18;
    private javax.swing.JButton jButton19;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton20;
    private javax.swing.JButton jButton21;
    private javax.swing.JButton jButton22;
    private javax.swing.JButton jButton23;
    private javax.swing.JButton jButton24;
    private javax.swing.JButton jButton25;
    private javax.swing.JButton jButton26;
    private javax.swing.JButton jButton27;
    private javax.swing.JButton jButton28;
    private javax.swing.JButton jButton29;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton30;
    private javax.swing.JButton jButton31;
    private javax.swing.JButton jButton32;
    private javax.swing.JButton jButton33;
    private javax.swing.JButton jButton34;
    private javax.swing.JButton jButton35;
    private javax.swing.JButton jButton36;
    private javax.swing.JButton jButton37;
    private javax.swing.JButton jButton38;
    private javax.swing.JButton jButton39;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton40;
    private javax.swing.JButton jButton41;
    private javax.swing.JButton jButton42;
    private javax.swing.JButton jButton43;
    private javax.swing.JButton jButton44;
    private javax.swing.JButton jButton45;
    private javax.swing.JButton jButton46;
    private javax.swing.JButton jButton47;
    private javax.swing.JButton jButton48;
    private javax.swing.JButton jButton49;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton50;
    private javax.swing.JButton jButton51;
    private javax.swing.JButton jButton52;
    private javax.swing.JButton jButton53;
    private javax.swing.JButton jButton54;
    private javax.swing.JButton jButton55;
    private javax.swing.JButton jButton56;
    private javax.swing.JButton jButton57;
    private javax.swing.JButton jButton58;
    private javax.swing.JButton jButton59;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton60;
    private javax.swing.JButton jButton61;
    private javax.swing.JButton jButton62;
    private javax.swing.JButton jButton63;
    private javax.swing.JButton jButton64;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    // End of variables declaration//GEN-END:variables
    //Semaphore movementAccessSemaphore = new Semaphore(1);
    private boolean positionChosen = false;
    private int[] movimiento;

    @SuppressWarnings("static-access")
    public void esperarMovimiento(JugadorGUIHandler handler) {
        mainThread = Thread.currentThread();
        try {
            mainThread.sleep(10000);
        } catch (InterruptedException ex) {
        // Don't do nothing, just pass it through.
        }

        if (positionChosen) {
            handler.setMovimiento(movimiento);
        } else {
            handler.setMovimiento(new int[]{0, 0, 0, 0});
        }
    }

    public void elegirPosicion(int buttonNumber, int destinationNumber) {
        movimiento = new int[]{buttonNumber / 8, buttonNumber % 8, destinationNumber / 8, destinationNumber % 8,};
        positionChosen = true;
        mainThread.interrupt();
    }
}

enum EstadoTablero {
	INICIO, ESPERANDO_ELECCION_BLANCO, ESPERANDO_ELECCION_FICHA, ESPERANDO_ELECCION_NUEVA_POSICION
}